package com.ipcamera.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DialogTitle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.ipcamer.demo.R;
import com.ipcamera.demo.adapter.MessageAdapter;
import com.ipcamera.demo.bean.ErrorBean;
import com.ipcamera.demo.bean.JsonBean;
import com.ipcamera.demo.bean.MessageBean;
import com.ipcamera.demo.bean.PushBindDeviceBean;
import com.ipcamera.demo.bean.SetLanguageBean;
import com.ipcamera.demo.net.ApiCallBack;
import com.ipcamera.demo.net.HttpConstances;
import com.ipcamera.demo.net.HttpHelper;
import com.ipcamera.demo.net.VcmApi;
import com.ipcamera.demo.utils.ContentCommon;
import com.ipcamera.demo.utils.EncryptionUtils;
import com.ipcamera.demo.utils.Log;
import com.ipcamera.demo.utils.StringUtils;
import com.ipcamera.demo.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class IpConnectActivity extends Activity implements View.OnClickListener {
    private EditText et_uid, et_token, et_oemid;
    private PushBindDeviceBean pushBindDeviceBean = null;

    private SetLanguageBean setLanguageBean = null;
    private Button btn_get_token, info_button, btn_getservicelist, btn_getdevnode, btn_setdevnode;
    // DemoPushService.class 自定义服务名称, 核心服务
    private Class userPushService = DemoPushService.class;

    private ListView mListview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ip_connect);
        /**
         *
         * 1，个推 配置 添加两个个推服务类DemoPushService和DemoPushService，直接复制到你的项目中
         * 2，在清单文件配置对应的 服务组件和个推对应的密钥配置
         * 3，配置对应的权限
         *
         *
         * 绑定接口顺序：
         * 1，调通“绑定接口”
         * 2，绑定成功后“设置语言接口”
         * 3，业务上删除摄像机后要调用 “解绑接口”
         *
         */
        initView();
        initPush();


    }

    private void initPush() {

        //初始化话个推
        PushManager.getInstance().initialize(this.getApplicationContext(), userPushService);
        //启动接收推送服务
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);
        //得到对应的token字符串
        et_token.setText(PushManager.getInstance().getClientid(this));
    }

    //初始化数据
    private void initData() {
        pushBindDeviceBean = new PushBindDeviceBean();
        String date = (System.currentTimeMillis() + "").substring(0, 10);
        pushBindDeviceBean.setDate(date);
        String ran = StringUtils.getFourRandom();
        pushBindDeviceBean.setRan(ran);
        pushBindDeviceBean.setEncryp(StringUtils.getEncryp("281e70f4-f9e8-211e-4bea-db24d44b1adf", date, ran));
        pushBindDeviceBean.setToken(et_token.getText().toString());
        pushBindDeviceBean.setUid(et_uid.getText().toString().toUpperCase());
        pushBindDeviceBean.setOemid(et_oemid.getText().toString());

        setLanguageBean = new SetLanguageBean();
        String date2 = (System.currentTimeMillis() + "").substring(0, 10);
        setLanguageBean.setDate(date2);
        String ran2 = StringUtils.getFourRandom();
        setLanguageBean.setRan(ran2);
        setLanguageBean.setEncryp(StringUtils.getEncryp("281e70f4-f9e8-211e-4bea-db24d44b1adf", date2, ran2));
        setLanguageBean.setToken(et_token.getText().toString());
        setLanguageBean.setUid(et_uid.getText().toString().toUpperCase());
        setLanguageBean.setOemid(et_oemid.getText().toString());
        //de en es fr it ja ko nl pl pt-br ru th vi zh zh_FT
        setLanguageBean.setLanguage("zh");


    }

    private void initView() {
        et_uid = (EditText) findViewById(R.id.et_uid);
        et_token = (EditText) findViewById(R.id.et_token);
        et_oemid = (EditText) findViewById(R.id.et_oemid);
        info_button = (Button) findViewById(R.id.getInfo);
        findViewById(R.id.btn_get_token).setOnClickListener(this);
        findViewById(R.id.btn_1).setOnClickListener(this);
        findViewById(R.id.btn_2).setOnClickListener(this);
        findViewById(R.id.btn_3).setOnClickListener(this);
        info_button.setOnClickListener(this);

        btn_getservicelist = (Button) findViewById(R.id.btn_getservicelist);
        btn_getservicelist.setOnClickListener(this);


        btn_getdevnode = (Button) findViewById(R.id.btn_getdevnode);
        btn_getdevnode.setOnClickListener(this);

        btn_setdevnode = (Button) findViewById(R.id.btn_setdevnode);
        btn_setdevnode.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_1:
                initData();
                if (check()) {
                    bindDevices();
                }

                break;
            case R.id.btn_2:
                initData();
                if (check()) {
                    unbindDevices();
                }

                break;

            case R.id.btn_3:
                initData();
                if (check()) {
                    setLanguage();
                }

                break;

            case R.id.btn_get_token:
                et_token.setText(PushManager.getInstance().getClientid(this));
                break;
            case R.id.getInfo:
                Intent i = new Intent(this, MessageActivity.class);
                startActivity(i);
                //getDevicesInfo();
                break;
            case R.id.btn_getservicelist:
                getsericelist();
                break;
            case R.id.btn_getdevnode:
                if (check()) {
                    getdevnode();
                }

                break;
            case R.id.btn_setdevnode:
                if (check()) {
                    setDevNode();
                }

                break;
        }
    }


    private  String getContent(String json) {
        StringBuilder sb = new StringBuilder();
        JSONObject user1 = new JSONObject();
        try {


            user1.put("did", et_uid.getText().toString().toUpperCase());
            user1.put("type", "alone");
            user1.put("host", "http://push-frankfurt.eye4.cn");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return (user1.toString());
    }

    //推送列表
    private void setDevNode() {

        VcmApi.get().posttoservece(HttpConstances.settingDevNode, getContent(""), new ApiCallBack() {
            @Override
            public void onFinish(JsonBean bean) {
                ToastUtils.show(IpConnectActivity.this, bean.getJson());
            }

            @Override
            public void onError(ErrorBean bean) {
                ToastUtils.show(IpConnectActivity.this, bean.getErrorMsg());
            }
        });
    }


    //推送列表
    private void getsericelist() {

        VcmApi.get().get(HttpConstances.GETSERIVELISTINFO, "", new ApiCallBack() {
            @Override
            public void onFinish(JsonBean bean) {
                ToastUtils.show(IpConnectActivity.this, bean.getJson());
            }

            @Override
            public void onError(ErrorBean bean) {
                ToastUtils.show(IpConnectActivity.this, bean.getErrorMsg());
            }
        });
    }


    //推送列表
    private void getdevnode() {

        VcmApi.get().get(HttpConstances.GETDEVLISTINFO + et_uid.getText().toString().toUpperCase(), "", new ApiCallBack() {
            @Override
            public void onFinish(JsonBean bean) {
                ToastUtils.show(IpConnectActivity.this, bean.getJson());
            }

            @Override
            public void onError(ErrorBean bean) {
                ToastUtils.show(IpConnectActivity.this, bean.getErrorMsg());
            }
        });
    }

    //设置语言接口
    private void setLanguage() {
        JSONObject result = new JSONObject();
        try
        {
            String time = (System.currentTimeMillis() + "").substring(0, 10);
            String nonce = StringUtils.getFourRandom();

            result.put("uid", et_uid.getText().toString().toUpperCase());
            result.put("phoneId", "123456789");
            result.put("oemid", et_oemid.getText().toString().toUpperCase());
            result.put("language", "en");
            result.put("encryp", StringUtils.getEncryp("3eb7f920-12ef-4c82-88de-32ffc6ba8f4b",time,nonce));
            result.put("ran", nonce);
            result.put("date", time);
        }catch (Exception e)
        {

        }
        VcmApi.get().bindV2(HttpConstances.v2_URL+"/setLanguage/v2", result.toString(), new ApiCallBack() {
            @Override
            public void onFinish(JsonBean bean) {
                ToastUtils.show(IpConnectActivity.this, bean.getJson());
            }

            @Override
            public void onError(ErrorBean bean) {
                ToastUtils.show(IpConnectActivity.this, bean.getErrorMsg());
            }
        });
    }

    //解绑接口
    private void unbindDevices() {

        JSONObject result = new JSONObject();
        try
        {
            String time = (System.currentTimeMillis() + "").substring(0, 10);
            String nonce = StringUtils.getFourRandom();

            result.put("uid", et_uid.getText().toString().toUpperCase());
            result.put("phoneId", "123456789");
            result.put("oemid", et_oemid.getText().toString().toUpperCase());
            result.put("pushToken", et_token.getText().toString());
            result.put("encryp", StringUtils.getEncryp("3eb7f920-12ef-4c82-88de-32ffc6ba8f4b",time,nonce));
            result.put("ran", nonce);
            result.put("date", time);
        }catch (Exception e)
        {

        }

        VcmApi.get().bindV2(HttpConstances.v2_URL+"/unbind/v2", result.toString(), new ApiCallBack() {
            @Override
            public void onFinish(JsonBean bean) {
                ToastUtils.show(IpConnectActivity.this, bean.getJson());
            }

            @Override
            public void onError(ErrorBean bean) {
                ToastUtils.show(IpConnectActivity.this, bean.getErrorMsg());
            }
        });
    }

    private boolean check() {
        if (textIsNull(et_oemid) && textIsNull(et_token) && textIsNull(et_uid)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean textIsNull(EditText editText) {
        if (editText.getText().toString().equals("")) {
            ToastUtils.show(IpConnectActivity.this, "存在输入框为空");
            return false;
        } else {
            return true;
        }
    }

    //绑定接口
    private void bindDevices() {
        JSONObject result = new JSONObject();
        try
        {
            String time = (System.currentTimeMillis() + "").substring(0, 10);
            String nonce = StringUtils.getFourRandom();

            result.put("uid", et_uid.getText().toString().toUpperCase());
            result.put("phoneId", "123456789");
            result.put("oemid", et_oemid.getText().toString().toUpperCase());
            result.put("pushToken", et_token.getText().toString());
            result.put("pushChannel", "geTui");
            result.put("language", "en");
            result.put("encryp", StringUtils.getEncryp("3eb7f920-12ef-4c82-88de-32ffc6ba8f4b",time,nonce));
            result.put("ran", nonce);
            result.put("date", time);
        }catch (Exception e)
        {

        }
        VcmApi.get().bindV2(HttpConstances.bingv2url, result.toString(), new ApiCallBack() {
            @Override
            public void onFinish(JsonBean bean) {
                ToastUtils.show(IpConnectActivity.this, bean.getJson());
            }

            @Override
            public void onError(ErrorBean bean) {
                ToastUtils.show(IpConnectActivity.this, bean.getErrorMsg());
            }
        });
    }


}
